package cn.itcast.searchapi;

import cn.itcast.documentapis.esClient;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.junit.jupiter.api.Test;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 15:50 2019/3/8
 * @Version 1.0
 **/
public class PrepareSearch {

    @Test
    public void prepareSc() {
        TransportClient client = esClient.getClient();
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("bank")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)

                .setPostFilter(QueryBuilders.rangeQuery("age").from(10).to(80))
                .setFrom(0).setSize(60).setExplain(true);
        System.out.println(searchRequestBuilder.toString());
        SearchResponse searchResponse = searchRequestBuilder.get();

        System.out.println(searchResponse.toString());

        do {
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                //Handle the hit...
            }

            searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
        } while(searchResponse.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.
    }
}
